package com.molyfun.parents.activity.course.view.courseitem;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.course.bean.CourseMediaBean;
import com.molyfun.parents.dao.entity.SingleDownloadTask;
import com.molyfun.parents.dao.gen.SingleDownloadTaskDao;
import com.molyfun.parents.service.MediaUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.molyfun.parents.utils.Const.VIDEO_COVER_SUFFIX;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class CourseVideoView extends BaseCourseView implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.course_cell_video_img)
    ImageView mCourseCellImg;
    @InjectView(R.id.course_cell_video_play)
    ImageView mPlay;

    private SingleDownloadTask mSingleCoverDownloadTask, mSingleVideoDownloadTask;

    public CourseVideoView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public CourseVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CourseVideoView(Context context) {
        super(context);
        init(context, null);
    }

    public void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.layout_course_cell_video, this);
        ButterKnife.inject(this);
        mCourseCellImg.setOnClickListener(this);
        mPlay.setOnClickListener(this);
    }

    /**
     * init view
     */
    public void initData(CourseMediaBean courseMediaBean) {
        super.initData(courseMediaBean);
        if (courseMediaBean == null) {
            return;
        }

        if (!TextUtils.isEmpty(courseMediaBean.getUrl())) {
            Picasso.with(getContext())
                    .load(courseMediaBean.getUrl() + VIDEO_COVER_SUFFIX)
                    .into(mCourseCellImg);
        }

//        if (!TextUtils.isEmpty(courseMediaBean.getCover())) {
//            if (mCourseMediaBean.isOffLineMode()) {
//                //TODO 本地视频图片
//                List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(courseMediaBean.getCover())).build().list();
//                if (singleDownloadTasks != null && singleDownloadTasks.size() > 0) {
//                    mSingleCoverDownloadTask = singleDownloadTasks.get(0);
//                }
//                if (mSingleCoverDownloadTask != null) {
//                    if (!TextUtils.isEmpty(mSingleCoverDownloadTask.getPath())) {
//                        mCourseCellImg.setImageURI(Uri.fromFile(new File(mSingleCoverDownloadTask.getPath())));
//                    } else {
//                        mCourseCellImg.setImageResource(R.mipmap.ic_video_holder);
//                    }
//                } else {
//                    mCourseCellImg.setImageResource(R.mipmap.ic_video_holder);
//                }
//            } else {
//
//                Picasso.with(context)
//                        .load(courseMediaBean.getCover())
//                        .placeholder(R.mipmap.ic_video_holder)
//                        .into(mCourseCellImg);
//
//            }
//        } else {
//            if (!TextUtils.isEmpty(courseMediaBean.getUrl())) {
//                Picasso.with(context)
//                        .load(courseMediaBean.getUrl() + "?vframe/png/offset/0/w/480/h/480")
//                        .placeholder(R.mipmap.ic_video_holder)
//                        .into(mCourseCellImg);
//            } else {
//                mCourseCellImg.setImageResource(R.mipmap.ic_video_holder);
//            }
//        }
        //需要根据音乐是否播放，注册音乐播放状态监听器

        mCourseCellImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.course_cell_video_img:
            case R.id.course_cell_video_play:
                if (mCourseMediaBean.isOffLineMode()) {
                    //TODO 本地视频
                    List<SingleDownloadTask> singleDownloadTasks = singleDownloadTaskDao.queryBuilder().where(SingleDownloadTaskDao.Properties.Url.eq(mCourseMediaBean.getUrl())).build().list();
                    if (singleDownloadTasks != null && singleDownloadTasks.size() > 0) {
                        mSingleVideoDownloadTask = singleDownloadTasks.get(0);
                    }
                    if (mSingleVideoDownloadTask != null) {
                        if (!TextUtils.isEmpty(mSingleVideoDownloadTask.getPath())) {
                            MediaUtils.startVideo(getContext(), mSingleVideoDownloadTask.getPath(), mCourseMediaBean.getGalleryname());
                        }
                    }
                } else {
                    MediaUtils.startVideoAndStopAudio(context, mCourseMediaBean.getUrl(), mCourseMediaBean.getGalleryname());
                }

                break;
        }
    }

}